package ltd.hyangyang.vegetable.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ltd.hyangyang.vegetable.entity.User;
import ltd.hyangyang.vegetable.entity.UserAnalysis;
import ltd.hyangyang.vegetable.mapper.UserMapper;
import ltd.hyangyang.vegetable.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getByAddr(String remoteAddr) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ip",remoteAddr);
        User user = this.baseMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public List<UserAnalysis> useInfo() {

        List<UserAnalysis> userAnalyses =userMapper.useInfo();

        System.out.println(JSON.toJSONString(userAnalyses));

        return userAnalyses;
    }

    @Override
    public Integer userVisits() {
        List<User> users = baseMapper.selectList(null);

        Integer userVisits = 0;
        for (User user : users) {
            userVisits = user.getUserVisits()+userVisits;
        }
        return userVisits;
    }

    @Override
    public Integer addUserNumber() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("create_time", LocalDate.now());
        int addUserNumber = baseMapper.selectList(userQueryWrapper).size();
        return addUserNumber;
    }
}
